package com.ouken.com;


import java.util.ArrayList;

import com.gen.tool.AlertDialog_ok;
import com.gen.tool.SoundPlayer;
import com.ouken.asynTask.AsynLogin;
//import com.ouken.tool.MplayerTool;
import com.ouken.tool.MyApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LoginActivity extends Activity {
    /** Called when the activity is first created. */
	private MyApp appState;
	private SharedPreferences userInfo;
	private RelativeLayout relativelayout_login_bg;
	private TextView lk_forgetpasswd;
	private ImageButton  button_login;
	private ImageButton button_register;
	private EditText et_email ;
	private EditText et_passwd;
	//private MplayerTool mplayerTool ;
	private AsynLogin asynLogin;
	
	private Context context;
	

	private void initView(){
		userInfo = getSharedPreferences("user_info", 0);
		appState = ((MyApp)getApplicationContext());
		//appState.setMusic(userInfo.getString("MUSIC", "ON").equals("ON"));
		appState.setListActivities(new ArrayList());
		appState.addActivity(LoginActivity.this);
		
		
	    relativelayout_login_bg = (RelativeLayout)findViewById(R.id.relativelayout_login_bg);
        lk_forgetpasswd = (TextView)findViewById(R.id.loginAvtivity_tv_forgetPasswd);
        button_login = (ImageButton)findViewById(R.id.loginAvtivity_ib_login);
        button_register = (ImageButton)findViewById(R.id.loginAvtivity_ib_register);
        et_email = (EditText)findViewById(R.id.loginAvtivity_et_email);
		et_passwd = (EditText)findViewById(R.id.loginAvtivity_et_passwd);
		
		context = LoginActivity.this;
		//mplayerTool = new MplayerTool(LoginActivity.this);

	}

	private void autoLogin(){
		if(userInfo.getString("AUTOLOGIN", "NO").equals("YES")){
			et_email.setText(userInfo.getString("EMAIL", ""));
			et_passwd.setText(userInfo.getString("PWD", ""));
			lk_forgetpasswd.setVisibility(View.GONE);
	        button_login.setVisibility(View.GONE);
	        button_register.setVisibility(View.GONE);
	        et_email.setVisibility(View.GONE);
			et_passwd.setVisibility(View.GONE);
			new AsynLogin(LoginActivity.this, et_email, et_passwd, appState).execute();
		}
	}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initView();
        autoLogin();
        
        
        
        relativelayout_login_bg.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(LoginActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
				
			}
		});
        
        lk_forgetpasswd.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SoundPlayer.playSound(R.raw.keypress_common);
				//mplayerTool.playButtonPressed(LoginActivity.this);
				Intent intent = new Intent(LoginActivity.this, ForgetPwdActivity.class);
				startActivity(intent);
			}
		});
        
        button_login.setOnClickListener(new View.OnClickListener() {
        	
			@Override
			public void onClick(View v) {
				((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(LoginActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
				SoundPlayer.playSound(R.raw.keypress_common);
				//mplayerTool.playButtonPressed(LoginActivity.this);
				
				String checkResult = "OK";
		        String errorMessage = "" ;
		        if(et_email.getText().length() == 0){
		        	checkResult = "ERROR";
		        	errorMessage += "email is empty!\n";
		        }
		        if(et_passwd.getText().length() == 0){
		        	checkResult = "ERROR";
		        	errorMessage += "password is empty!\n";
		        }
		        
		        if(checkResult.equals("ERROR")){
		        	AlertDialog_ok myDialog = new AlertDialog_ok(LoginActivity.this,errorMessage);
		        	final AlertDialog now = myDialog.AlertDialog_ok_show();
				    
		        	myDialog.getConfirm_btn_ok().setOnClickListener(new View.OnClickListener(){
						@Override  
					    public void onClick(View v) {  
							SoundPlayer.playSound(R.raw.keypress_common);
							  now.dismiss(); 
							} 
						}
					);  	
					return;
					
		        }

				asynLogin = new AsynLogin(LoginActivity.this, et_email, et_passwd, appState);
				asynLogin.execute();
				
				
			}
				
			
		});
        
        button_register.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SoundPlayer.playSound(R.raw.keypress_common);
				//mplayerTool.playButtonPressed(context);
				 
				//startService(new Intent("com.serive.refreshroomlist"));
				Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
				/*AlertDialog_ok_cancel myDialog = new AlertDialog_ok_cancel(LoginActivity.this,"second try");
	        	final AlertDialog now = myDialog.AlertDialog_ok_cancel_show();
			    
	        	myDialog.getConfirm_btn_ok().setOnClickListener(new View.OnClickListener(){
					@Override  
				    public void onClick(View v) {  
						  now.dismiss(); 
						  System.out.println("2 OK dismissOK");
						} 
					}
				); 
				
	        	myDialog.getCancel_btn_can().setOnClickListener(new View.OnClickListener(){
					@Override  
				    public void onClick(View v) {  
						  now.dismiss(); 
						  System.out.println("2 Cancel dismissOK");
						} 
					}
				);*/
			}
		});
        
    }
    
   
}